<?php
/**
*			翻页类
*			$page=new page($totalRow,$listRow);
*			$page->page_one();样式一
*			$this->fistrow();返回起始行
*			$this->lastrow();返末尾行
*
***/
	class page{
	
		private $firstPage ="首页"; 
		private $lastPage="最后一页";    
		private $prev="上一页";  
		private $next ="下一页";    
		private $header ="条记录";    
		private $totalPage ; //总页数
		public  $firstRow="1"  ; //起始行 
		private $lastRow  ;  //末尾行
		private $totalRow ;  //总行数  
		private $listRow  ;  //每页显示的行数
		private $nowPage ;  //当前页
		private $pagenav="";  //html
		private $pagenav2="";  //html
		private $pagenav3="";  //html
		private $url;  //html
		private $nextPage;
		private $prevPage;
		private $centerPage="1";
		private $t="0";
		
		
		
		/**
		*	$totalPage  总条数
		*	$listRow    每页显示条数
		*   $firstRow   第一行
		*/
		function __construct($totalRow,$listRow){
			
			$this->totalRow=$totalRow;
			
			$this->listRow=$listRow;
			
			$this->firstRow="1";
			
			$this->nowPage="1";
		}
		
		
		//----------翻页样式1---------------
		function page_one(){
		
			$this->nowPage=$this->nowPage();
			
			$this->url=$this->url();
			
			$this->totalPage=$this->setTotalPage();
			
			$this->pagenav.="<font>".$this->totalRow.$this->header."</font>";
			//首页 上一页
			if($this->nowPage>1&&$this->totalPage>1){
				
				$this->prevPage=$this->nowPage-1;
				
				$this->pagenav.="<a href='".$this->url."?&page=1'>".$this->firstPage."</a>";
				
				$this->pagenav.="<a href='".$this->url."?&page=".$this->prevPage."'>".$this->prev."</a>";
				
			}
			
			//中间循环
			if($this->nowPage<10){
			
				if($this->totalPage>10){
				
					for($i=1;$i<=10;$i++){
						
						$this->pagenav.=" <a href='".$this->url."?&page=".$i."'>".$i."</a>";
					}
				}else{
				
					for($i=1;$i<=$this->totalPage;$i++){
						
						$this->pagenav.=" <a href='".$this->url."?&page=".$i."'>".$i."</a>";
					}
				}
			}
			
			if($this->nowPage>=10){
			
				if($this->totalPage-$this->nowPage<=5){
					
					for($i=$this->totalPage-9;$i<=$this->totalPage;$i++){
						
						$this->pagenav.=" <a href='".$this->url."?&page=".$i."'>".$i."</a>";
					}
				}else{
				
					for($i=-4;$i<=5;$i++){
					
						$this->centerPage= $this->nowPage+$i;
						
						$this->pagenav.=" <a href='".$this->url."?&page=".$this->centerPage."'>".$this->centerPage."</a>";
					}
				}
			}
			//尾页 下一页
			if($this->nowPage < $this->totalPage){
			
				$this->nextPage=$this->nowPage+1;
				
				$this->pagenav.="<a href='".$this->url."?&page=".$this->nextPage."'>".$this->next."</a>";
				
				$this->pagenav.="<a href='".$this->url."?&page=".$this->totalPage."'>".$this->lastPage."</a>";
				
				$this->nowPage++;
			}
			
			echo $this->pagenav;
		}
		//----------翻页样式2---------------
		function page_two(){
		
			$this->nowPage=$this->nowPage();
			
			$this->url=$this->url();
			
			$this->totalPage=$this->setTotalPage();
		
			$this->pagenav2 .= "到第<select name='topage' onchange='window.location=\"$this->url?&amp;page=\"+this.value'>\n";
			
			if($this->totalPage==0){
			
				$this->pagenav2 .= "<option value='1'>1</option>\n";
			}
			
			for ($i = 1; $i <= $this->totalPage; $i++) {
			
				if ($i == $this->nowPage)
					$this->pagenav2 .= "<option value='$i' selected=\"selected\">$i</option>\n";  //选项停在当前页
				else
					$this->pagenav2 .= "<option value='$i'>$i</option>\n";
					
			}
			$this->pagenav2 .= "</select>";
			
			echo $this->pagenav2;
		
		}
		//----------翻页样式3---------------
		function page_three(){
			
			$this->url=$this->url();
			
			$this->pagenav3="<div><form action='PageClass.php' method='POST'><input type='text' name='page'><input type='submit'></form></div>";
			
			echo $this->pagenav3;
		
		}
		/**
		*		获取当前页
		*/
		function nowPage(){
		
			$this->nowPage=isset($_POST['page'])?$_POST['page']:(isset($_GET['page'])?$_GET['page']:'1');
			
			
			
			return $this->nowPage;
		}
		/**
		*		获取当前url页
		*/
		function url(){
			
			$this->url =  $_SERVER["REQUEST_URI"]; //获取当前url
			
			$this->url=explode("?&page",$this->url);
			
			return $this->url=$this->url[0];
		
		}
		/**
		*   $totalRow  总行数
		*	$totalPage 返回总页数
		*/
		function setTotalPage(){
		
			$this->totalPage=ceil($this->totalRow/$this->listRow);
			
			return $this->totalPage;
		}
		
		/**
		*   $firstRow  开始行数
		*/
		function firstRow(){
			
				$this->firstRow=($this->nowPage()-1)*$this->listRow+1;
	
			return $this->firstRow;
		}
		/**
		*   $lastRow  结尾行数
		*/
		function lastRow(){
			
			$this->lastRow=$this->firstRow()+$this->listRow-1;
			
			return $this->lastRow;
		}
		
		/**
		*	$header  设置总条数
		*/
		function setHeader($header){
		
			$this->header=$header;
		}
		
		/**
		*   $firstPage  设置首页标志
		*/
		function setFirstPage($firstPage){
		
			$this->firstPage=$firstPage;
		}
		
		/**
		*   $lastPage  设置尾页标志
		*/
		function setLastPage($lastPage){
		
			$this->lastPage=$lastPage;
		}
		
		/**
		*   $lastPage  设置上一页标志
		*/
		function setPrev($prev){
		
			$this->prev=$prev;
		}
		
		/**
		*   $lastPage  设置下一页标志
		*/
		function setNext($next){
		
			$this->next=$next;
		}
	
	}
	
	$page=new page(188,10);
	$page->page_one();
	$page->page_two();
	$page->page_three();
	echo "<br/>","fistrow",$page->firstRow(),"<br/>";
	echo "lastrow",$page->lastRow(),"<br/>";

?>